<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Ordervtemp');
Globals::requireTable('Account');
Globals::requireTable('Msg');
Globals::requireTable('Withdrawals');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');

class PayvmoneyController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $OrdervtempTable;
	protected $AccountTable;
	protected $MsgTable;
	protected $WithdrawalsTable;
	protected $VmoneyTable;
	protected $VlevelTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable          = new UserTable($config);
		$this->ProductTable       = new ProductTable($config);
		$this->OrderTable         = new OrderTable($config);
		$this->OrdervtempTable    = new OrdervtempTable($config);
		$this->AccountTable       = new AccountTable($config);
		$this->MsgTable           = new MsgTable($config);
		$this->WithdrawalsTable   = new WithdrawalsTable($config);
		$this->VmoneyTable        = new VmoneyTable($config);
		$this->VlevelTable        = new VlevelTable($config);
	}
	
	/**
	 * 下单
	 * uid/token&machine_type
	 * vmoney
	 * money（v币*0.95）
	 */
	public function indexAction()
	{
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    $user = $this->UserTable->getRow($uid);
	    if($uid < 1 || $user['id'] < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>''));
	        exit();
	    }
	    
	    $time = time();
	    
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['createtime'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $data['vmoney'] = $this->getParam('vmoney');
	    $data['money'] = $data['vmoney'] * 0.95;
	    $this->OrdervtempTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>(string)$data['orderid']));
	    exit();
	}
	
	/**
	 * 支付成功
	 * orderid
	 * payorderid
	 * paytype
	 * sign
	 */
	public function payokAction()
	{
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('ordersign');
	    $time = time();
	    
	    $order_vtemp = $this->OrdervtempTable->getRow(array('orderid'=>$orderid));
	    //$order = $this->OrderTable->getRow(array('orderid'=>$orderid));
	    $user_buyer = $this->UserTable->getRow($order_vtemp['uid']);//账号购买者
	    
	    if($user_buyer['id'] < 1 || $order_vtemp['id'] < 1 || $order_vtemp['status'] != 0){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $new_num_arr = array();
	    $orderid_num = $orderid + 19921206;
	    for ($ii = 0 ; $ii < strlen($orderid_num) ; $ii++){
	        $tmp_num = substr($orderid_num, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    if($sign != $new_num){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
		
		$alipay = new Alipay();
	    $re = $alipay->queryone('',$payorderid);
	    $alipay_money = $re->response->trade->total_fee;
	    if($order_vtemp['money'] != $alipay_money){
	        echo json_encode(array('returncode'=>-999,'username'=>'','password'=>''));
	        exit();
	    }

	    switch ($paytype){
	        case 'alipay':
	            $paytypevalue = 1;
	            break;
	        default:
	            $paytypevalue = 1;
	            break;
	    }
	    $data['paytype'] = $paytypevalue;
	    $data['paytime'] = date('Y-m-d H:i:s');
	    $data['payorderid'] = $payorderid;
	    $data['status'] = 1;
	    $this->OrdervtempTable->update($data,$order_vtemp['id']);
	    
	    
	    $order_data['orderid'] = $order_vtemp['orderid'];
	    $order_data['uid'] = $order_vtemp['uid'];
	    $order_data['pid'] = 0;
	    $order_data['aid'] = 0;
	    $order_data['uid_from'] = 0;
	    $order_data['createtime'] = $order_vtemp['createtime'];
	    $order_data['status'] = $order_vtemp['status'];
	    $order_data['paytime'] = $order_vtemp['paytime'];
	    $order_data['payorderid'] = $order_vtemp['payorderid'];
	    $order_data['paytype'] = $order_vtemp['paytype'];
	    $order_data['money'] = $order_vtemp['money'];
	    $order_data['days'] = 0;
	    $order_data['endtime'] = 0;
	    $order_data['type'] = 2;
	    $this->OrderTable->add($order_data);
	    
	    
	    $vmoney_data['uid'] = $order_vtemp['uid'];
	    $vmoney_data['money'] = $order_vtemp['vmoney'];
	    $vmoney_data['type'] = 3;
	    $vmoney_data['remark'] = 'v币充值获得'.$order_vtemp['vmoney'];
	    $this->VmoneyTable->add($vmoney_data);
	    
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('PayvmoneyController', 'Controller');
